package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Finance;
import cn.wolfcode.trip.base.domain.ReturnMoney;
import cn.wolfcode.trip.base.mapper.FinanceMapper;
import cn.wolfcode.trip.base.mapper.ReturnMoneyMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class FinanceServiceImpl implements IFinanceService{

    @Autowired
    private FinanceMapper financeMapper;

    @Autowired
    private ReturnMoneyMapper returnMoneyMapper;

    @Autowired
    private ReturnMoney returnMoney;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void insert(Finance finance) {
        financeMapper.insert(finance);
    }

    @Override
    public void update(Long id, BigDecimal money) {
        returnMoney.setReturnTime(new Date());
        returnMoney.setUser(userMapper.selectByPrimaryKey(id));
        returnMoney.setReturnMoney(money);
        returnMoneyMapper.insert(returnMoney);
        Finance finance = financeMapper.selectByPrimaryKey(id);
        BigDecimal borrowMoney = finance.getBorrowMoney();
        BigDecimal money1 = borrowMoney.subtract(money);
        //finance.setBorrowMoney(money1);
        financeMapper.updateByUserId(id,money1);

    }

    @Override
    public Finance get(Long userId) {
        return financeMapper.selectByUserId(userId);
    }
}
